/*
 * Created on Sep 2, 2004
 *  
 */
package com.tgjorgoski.spellcheck.highlight;
import java.awt.*;
import javax.swing.text.*;
/**
 * Highlights using zig-zag underling in specific color
 * Height of the zig-zag and the placement of the underline can also be
 * set using the constructor
 * 
 * @author Tanasije Gjorgoski <tanas@ameritege.com>
 */
public class HLPainterUnderlineZigZag extends LayeredHighlighter.LayerPainter {
	int height;
	float dash[][];
	Stroke strokes[];
	int position;
	
	public HLPainterUnderlineZigZag(Color c, int height, int position) {
		color = c;
		this.height=height;
		dash= new float[height][];
		dash[0]=new float[2];
		dash[0][0]=1; dash[0][1]=2*height-3;
		dash[height-1]=new float[4];
		dash[height-1][0]=0; dash[height-1][1]=height-1;
		dash[height-1][2]=1; dash[height-1][3]=height-2;
		for(int i=1; i<height-1; i++) {
			dash[i]=new float[6];
			dash[i][0]=0;
			dash[i][1]=i;
			dash[i][2]=1;
			dash[i][3]=2*height-3-2*i;
			dash[i][4]=1;
			dash[i][5]=i-1;
		}
		strokes= new Stroke[height];
		for (int i=0; i<height; i++) {
			strokes[i]=new BasicStroke((float)0.01,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL,0,dash[i],0);
		}
		this.position=position;
	}
	/**
	 * Returns the color of the highlight.
	 * 
	 * @return the color
	 */
	public Color getColor() {
		return color;
	}
	// --- HighlightPainter methods ---------------------------------------
	/**
	 * Paints a highlight.
	 * 
	 * @param g
	 *            the graphics context
	 * @param offs0
	 *            the starting model offset >= 0
	 * @param offs1
	 *            the ending model offset >= offs1
	 * @param bounds
	 *            the bounding box for the highlight
	 * @param c
	 *            the editor
	 */
	public void paint(Graphics g, int offs0, int offs1, Shape bounds,
			JTextComponent c) {
	}
	
	/**
	 * Paints a portion of a highlight.
	 * 
	 * @param g
	 *            the graphics context
	 * @param offs0
	 *            the starting model offset >= 0
	 * @param offs1
	 *            the ending model offset >= offs1
	 * @param bounds
	 *            the bounding box of the view, which is not necessarily the
	 *            region to paint.
	 * @param c
	 *            the editor
	 * @param view
	 *            View painting for
	 * @return region drawing occured in
	 */
	public Shape paintLayer(Graphics g, int offs0, int offs1, Shape bounds,
			JTextComponent c, View view) {
		Color bupColor = c.getForeground(); 
		Color color = getColor();
		g.setColor(color);
		try {
			// --- determine locations ---
			Shape shape = view.modelToView(offs0, Position.Bias.Forward,
					offs1, Position.Bias.Backward, bounds);
			Rectangle r = (shape instanceof Rectangle)
					? (Rectangle) shape
					: shape.getBounds();
					
			Stroke backup = ((Graphics2D)g).getStroke();
			for(int i=0; i<height; i++) {
				((Graphics2D)g).setStroke(strokes[i]);
				g.drawLine(r.x, r.y+r.height-i+position, r.width+r.x, r.y+r.height-i+position);
			}
			((Graphics2D)g).setStroke(backup);
			
			//g.fillRect(r.x, r.y, r.width, r.height);
			return r;
		} catch (BadLocationException e) {
			// can't render
		}
		g.setColor(bupColor);
		// Only if exception
		return null;
	}
	private Color color;
}